package com.le.bbs.controller;

import com.le.api.BoardReplyApi;
import com.le.api.BoardTopicApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("count")
public class ReplyCountController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate; // 需要配置 Redis数据库

    @Autowired
    private BoardReplyApi boardReplyApi;

    /**
     * 取消评论点赞（目前只取消用户里的记录
     */
    @RequestMapping(value = "clearReplyZan", method = RequestMethod.GET)
    public Map<String, Object> clearReplyZan(@RequestParam String type,
                                             @RequestParam Integer uid,
                                             @RequestParam Integer replyid,
                                             @RequestParam Integer topicid){
        Map<String, Object> result = new HashMap<>();
        // 检测键 在Redis数据库中是否存在
        if (this.redisTemplate.opsForHash().hasKey("bbs-count", uid+"_"+topicid+"_"+type)){
            // 获取("cart_" + bearerToken)键的 哈希表的键集合 // 取出此人（bearerToken）所有的购物项
            List replyList = (List) this.redisTemplate.opsForHash().get("bbs-count", uid+"_"+topicid+"_"+type);

            if (!replyList.contains(replyid)){
                result.put("code", 0);
                result.put("msg", "已经取消了！");
                result.put("data", replyList);
                return result;
            }

            replyList.removeAll(Collections.singleton(replyid));

            // 存入数据
            this.redisTemplate.opsForHash().put("bbs-count", uid+"_"+topicid+"_"+type, replyid);

            result.put("code", 1);
            result.put("msg", "取消成功！");
            result.put("data", replyList);
        } else {
            result.put("code", 0);
            result.put("msg", "取消操作异常，请联系管理员！");
        }
        return result;
    }


    /**
     * 记录评论点赞
     */
    @RequestMapping(value = "zanReply", method = RequestMethod.GET)
    public Map<String, Object> zanTopic(@RequestParam String type,
                                        @RequestParam Integer uid,
                                        @RequestParam Integer topicid,
                                        @RequestParam Integer replyid){
        Map<String, Object> result = new HashMap<>();

        Map<String, Object> resultMap = this.boardReplyApi.isReplyId(replyid);
        if ("0".equalsIgnoreCase(resultMap.get("code").toString())){
            result.put("code", 0);
            result.put("msg", "查无此评论" + replyid);
            return result; // 业务问题，不是http的问题 返回200
        }

        // 添加点赞
        // 取出这个主题有哪些用户点赞
        List uidList = (List) this.redisTemplate.opsForHash().get("bbs-count", topicid+"_"+replyid+"_"+type);

        List replyList = (List) this.redisTemplate.opsForHash().get("bbs-count", uid+"_"+topicid+"_"+type);

        if (uidList == null){
            uidList = new ArrayList<>();
        }
        uidList.add(uid);

        if (replyList == null){
            replyList = new ArrayList<>();
        }
        replyList.add(replyid);

        // 存入数据
        this.redisTemplate.opsForHash().put("bbs-count", topicid+"_"+replyid+"_"+type, uidList);
        this.redisTemplate.opsForHash().put("bbs-count", uid+"_"+topicid+"_"+type, replyList);

        result.put("code", 1);
        result.put("num", uidList.size());

        return result;
    }
    /**
     * 获取评论点赞数
     */
    @RequestMapping(value = "getReplyZan", method = RequestMethod.GET)
    public Map<String, Object> getReplyZan(@RequestParam String type,
                                           @RequestParam Integer replyid,
                                           @RequestParam Integer topicid){
        Map<String, Object> result = new HashMap<>();
        if (this.redisTemplate.opsForHash().hasKey("bbs-count", topicid+"_"+replyid+"_"+type)){
            // entries() 取键值对（全部取到
            List uidList = (List) this.redisTemplate.opsForHash().get("bbs-count", topicid+"_"+replyid+"_"+type);
            result.put("code", 1);
            result.put("num", uidList.size());
        } else {
            result.put("code", 0);
            result.put("num", 0);
        }
        return result;
    }

    /**
     * 批量获取评论点赞数据
     */
    @RequestMapping(value = "getReplyZans", method = RequestMethod.POST)
    public Map<String, Object> getReplyZans(Integer[] replyids, Integer topicid){

        String type = "zan";
        Map<String, Object> result = new HashMap<>();

        for (Integer replyid: replyids){
            Map<String, Object> replyZan = getReplyZan(type, replyid, topicid);
            result.put(replyid+"", replyZan.get("num"));
        }
        return result;
    }

}
